package cn.com.controller;

import cn.com.Authorization;
import cn.com.beans.*;
import cn.com.service.BatchLineService;
import cn.com.service.CollegeInfoService;
import cn.com.service.FractionInfoService;
import cn.com.service.ScoreLineService;
import cn.com.util.Arithmetic;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;


/**
 * 预测分数页面控制器
 * @author 龙战广
 */
@Controller
@RequestMapping("/prediction")
public class PredictionController {

    @Resource
    private CollegeInfoService collegeInfoService;
    @Resource
    private ScoreLineService scoreLineService;
    @Resource
    private FractionInfoService fractionInfoService;
    @Resource
    private BatchLineService batchLineService;

    @GetMapping
    @Authorization(url = "/prediction",authority = "r",methods = RequestMethod.GET)
    public String home(){
        return "predictionRrecommended/prediction";
    }

    /**
     * 模糊查询学校
     * @param schoolName 学校关键字
     * @return 查询出的学校实体集合
     */
    @GetMapping("/school/{schoolName}")
    @ResponseBody
    public List<CollegeInfo> getName(@PathVariable String schoolName) {
        CollegeInfo keyword = new CollegeInfo();
        keyword.setCoName(schoolName);
        System.out.println(schoolName);
        List<CollegeInfo> schoolNames = collegeInfoService.fuzzyQueryAll(keyword);
        return schoolNames;
    }

    /**
     * 预测返回
     * @param coidValue 学校id
     * @param subjectValue 学科类型
     * @param scoreValue 分数
     * @return 预测概率
     */
    @GetMapping("/forecast/{coidValue}/{subjectValue}/{scoreValue}")
    @ResponseBody
    public List<Style> forecast(@PathVariable("coidValue") Integer coidValue, @PathVariable("subjectValue") Integer subjectValue, @PathVariable("scoreValue") Integer scoreValue){
        ScoreLine scoreLine = new ScoreLine();
        scoreLine.setScCoid(coidValue);
        scoreLine.setScType(subjectValue);
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        scoreLine.setScYear(year);
        ScoreLine probability= scoreLineService.findAll(scoreLine);
        Integer grade;
        List<Integer> academyAdmiIonlines = new ArrayList<>();
        List<Integer> gradeAdmiIonlines = new ArrayList<>();
        Style style = new Style();
        List<Style> styles = new ArrayList<>();
        Integer forecast;
        if(probability == null){
            int year2 ;
            FractionInfo fractionInfo = new FractionInfo();
            for(int i = 1; i<=5;i++){
                year2 = year-i;
                fractionInfo.setFrCoid(coidValue);
                fractionInfo.setFrType(subjectValue);
                fractionInfo.setFrYear(year2);
                FractionInfo info = fractionInfoService.findAll(fractionInfo);
                if(info == null){
                    System.out.println("分数表");
                    style.setState(0);
                    style.setStyle("系统错误，请向管理员反馈");
                    styles.add(style);
                    return styles;
                }
                academyAdmiIonlines.add(info.getFrScore());
            }
            CollegeInfo collegeInfo = collegeInfoService.selectByPrimaryKey(coidValue);
            String coBatch = collegeInfo.getCoBatch();
            BatchLine batchLine = new BatchLine();
            String Cotype;
            if(subjectValue == 0){
                Cotype = "文科";
            }else {
                Cotype = "理科";
            }
            for(int i = 1; i<=5;i++){
                year2 = year-i;
                batchLine.setBaYear(year2);
                batchLine.setBaType(Cotype);
                batchLine.setBaProvince("湖南");
                BatchLine batch = batchLineService.findBatch(batchLine);
                if(batch == null){
                    System.out.println("批次线15-19");
                    style.setState(0);
                    style.setStyle("系统错误，请向管理员反馈");
                    styles.add(style);
                    return styles;
                }
                if(coBatch.equals("本科一批")){
                    gradeAdmiIonlines.add(batch.getBaOne());
                }else if(coBatch.equals("本科二批")){
                    gradeAdmiIonlines.add(batch.getBaTwo());
                }else{
                    gradeAdmiIonlines.add(batch.getBaThree());
                }
            }
            batchLine.setBaYear(2020);
            batchLine.setBaType(Cotype);
            batchLine.setBaProvince("湖南");
            BatchLine batch = batchLineService.findBatch(batchLine);
            if(batch == null){
                System.out.println("批次线20");
                style.setState(0);
                style.setStyle("系统错误，请向管理员反馈");
                styles.add(style);
                return styles;
            }
            Integer score;
            if(coBatch.equals("本科一批")){
                score=batch.getBaOne();
            }else if(coBatch.equals("本科二批")){
                score=batch.getBaTwo();
            }else{
                score=batch.getBaThree();
            }
            System.out.println(score);
            forecast = Arithmetic.forecastScore(gradeAdmiIonlines, academyAdmiIonlines, 5, score);
            System.out.println(forecast);
            ScoreLine scoreLine1 = new ScoreLine();
            scoreLine1.setScCoid(coidValue);
            scoreLine1.setScYear(year);
            scoreLine1.setScType(subjectValue);
            scoreLine1.setScScore(forecast);
            scoreLineService.insert(scoreLine1);
            grade = forecast;
        }else{
            grade = probability.getScScore();
        }
        int probabilityValue =Arithmetic.probability(grade,scoreValue);
        if(probabilityValue>=80){
            style.setStyle("green");
        }else if(50<=probabilityValue && probabilityValue<80){
            style.setStyle("yellow");
        }else if(20<probabilityValue && probabilityValue<50){
            style.setStyle("orange");
        }else{
            style.setStyle("red");
        }
        style.setProbability(probabilityValue);
        style.setState(1);
        styles.add(style);
        return styles;
    }
}
